#!/bin/bash
# Copyright (c) 2017 ~ 2025, the original author wangl.sir individual Inc,
# All rights reserved. Contact us 983708408@qq.com
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ad. https://github.com/wl4g/super-devops or https://gitee.com/wl4g/super-devops
# @see https://yq.aliyun.com/articles/741365
# @see https://www.jianshu.com/p/f6437e914584
# @see https://wang4ever.lofter.com/post/1cca927e_1c752cc99
#
# Version v2.0
set -e

if [ "$USER" != "root" ]; then
  echo "Use the root user to execution this script!"
  exit 2
fi

MYSQL_BASE_DIR="$(cd "`dirname "$0"`"/..; pwd)"
cd $MYSQL_BASE_DIR


#
# Stopping of mysqld system service.
#
PIDS=`/bin/echo $(ps -ef|grep ${MYSQL_BASE_DIR}/bin/mysqld|grep -v grep|cut -c 9-15)`
if [ -n "$PIDS" ]; then
  while true
  do
    read -t 10 -p "=> $(date +'%y/%m/%d %H:%M:%S') [WARN] MySQL Server on '${MYSQL_BASE_DIR}/bin/mysqld' is running, \
pids: '${PIDS}', [Danger]you must stop it first? (y|yes|n|no)" cover
    if [ -n "$(echo $cover|egrep -i 'Y|YES')" ]; then # Ignore case
      break;
    elif [ -n "$(echo $cover|egrep -i 'N|NO')" ]; then
      exit 0;
    else
      echo "Please reenter it!"
    fi
  done

  echo -e "=> $(date +'%y/%m/%d %H:%M:%S') [INFO] Stopping MySQL Server on ${PIDS} ..."
  if [[ -f /etc/systemd/system/mysqld.service || -f /lib/systemd/system/mysqld.service ]]; then
    systemctl stop mysqld.service
  elif [ -f /etc/init.d/mysqld.service ]; then
    /etc/init.d/mysqld.service stop
  fi
fi


#
# Disable mysqld systemctl service.(if necessary)
#
if [ -f /etc/systemd/system/mysqld.service ]; then
  echo -e "=> $(date +'%y/%m/%d %H:%M:%S') [INFO] UnInstall MySQL System Service ..."
  sudo /bin/sh -c "exec systemctl disable mysqld"
fi


#
# Remove mysqld system service.
#
if [ -f /etc/systemd/system/mysqld.service ]; then # e.g: Ubuntu16.x+
  /bin/echo -e "=> $(date +'%y/%m/%d %H:%M:%S') [INFO] UnInstall Mysqld service of '/etc/systemd/system/mysqld.service' ..."
  /bin/rm -rf /etc/systemd/system/mysqld.service
elif [ -f /lib/systemd/system/mysqld.service ]; then # e.g: CenOS7.x, Ubuntu16.x
  echo -e "=> $(date +'%y/%m/%d %H:%M:%S') [INFO] UnInstall Mysqld service of '/lib/systemd/system/mysqld.service' ..."
  /bin/rm -rf /lib/systemd/system/mysqld.service
elif [ -f /etc/init.d/mysqld.service ]; then # CentOS6.x-
  echo -e "=> $(date +'%y/%m/%d %H:%M:%S') [INFO] UnInstall Mysqld service of '/etc/init.d/mysqld.service' ..."
  /bin/rm -rf /etc/init.d/mysqld.service
fi
# Generic
if [ -f /etc/systemd/system/multi-user.target.wants/mysqld.service ]; then
  echo -e "=> $(date +'%y/%m/%d %H:%M:%S') [INFO] UnInstall Mysqld service of '/etc/systemd/system/multi-user.target.wants/mysqld.service' ..."
  /bin/rm -rf /etc/systemd/system/multi-user.target.wants/mysqld.service
fi

#
# Cleanup server data files.
if [ -d "data/" ]; then
  while true
  do
    read -t 10 -p "=> $(date +'%y/%m/%d %H:%M:%S') [WARN] Do you need to remove the data directory on '${MYSQL_BASE_DIR}/data' \
to uninstall MySQL completely? It cannot be recovered after deletion. [Danger] Do you want to confirm deletion? (y|yes|n|no)" cover
    if [ -n "$(echo $cover|egrep -i 'Y|YES')" ]; then # Ignore case
      break;
    elif [ -n "$(echo $cover|egrep -i 'N|NO')" ]; then
      exit 0;
    else
      echo "Please reenter it!"
    fi
  done
  /bin/echo -e "=> $(date +'%y/%m/%d %H:%M:%S') [INFO] Force cleanup MySQL Server Data dir files ..."
  `/bin/rm -rf data/ conf/ run/ log/`
fi
# Fulsh systemd dbus. (if necessary)
if [ -f /bin/systemctl ]; then
  systemctl daemon-reload
fi


echo -e "=> $(date +'%y/%m/%d %H:%M:%S') [INFO] UnInstall successfully!"
